package com.angis.fx.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;


public class UserInfoDBHelper extends ObjectDBHelper {

	public static final String PK_USERNAME = "username";
	public static final String KEY_USERNAMECN = "usernamecn";
	public static final String KEY_USERPASSWORD = "userpassword";
	public static final String KEY_DEPARTMARTID = "departmartid";
	public static final String KEY_USERLAYER = "userlayer";
	public static final String KEY_MEMBERINFOS = "memberinfos";
	public static final String KEY_DEVICENO = "deviceno";
	private static final String TABLE_USERINFO = "userinfo";

	public UserInfoDBHelper(Context context) {
		super(context);
	}

	// ---向数据库中插入一个用户记录---
	public long insertUserInfo(String pUsername, String pUsernamecn,
			String pPwd, String pDepId, String pUserlayer, String pMemeberinfos, String pDeviceno) {
		ContentValues lInitialValues = new ContentValues();
		lInitialValues.put(PK_USERNAME, pUsername);
		lInitialValues.put(KEY_USERNAMECN, pUsernamecn);
		lInitialValues.put(KEY_USERPASSWORD, pPwd);
		lInitialValues.put(KEY_DEPARTMARTID, pDepId);
		lInitialValues.put(KEY_USERLAYER, pUserlayer);
		lInitialValues.put(KEY_MEMBERINFOS, pMemeberinfos);
		lInitialValues.put(KEY_DEVICENO, pDeviceno);
		return insert(TABLE_USERINFO, lInitialValues);
	}

	// ---删除一个指定用户的记录---
	public boolean delelteUserInfo(String pUsername) {
		if (writeDB == null)
			writeDB = dbHelper.getWritableDatabase();
		return writeDB.delete(TABLE_USERINFO, PK_USERNAME + "=" + pUsername, null) > 0;
	}

	// ---检索一个指定用户记录---
	public Cursor getSingleUserInfo(String pUsername, String pPwd) {
		String params[] = new String[] { pUsername, pPwd };
		if (readDB == null)
			readDB = dbHelper.getReadableDatabase();
		return readDB.query(TABLE_USERINFO, new String[] { PK_USERNAME,
				KEY_USERNAMECN, KEY_USERPASSWORD, KEY_DEPARTMARTID,
				KEY_USERLAYER, KEY_MEMBERINFOS, KEY_DEVICENO},
				"username=? and userpassword=?", params, null, null, null);
	}

	// 更新指定的用户记录
	public boolean updateUserInfo(String pUsername, String pUsernamecn,
			String pPwd, String pDepId, String pUserlayer, String pMemeberinfos, String pDeviceno) {
		String params[] = new String[]{pUsername};
		ContentValues lInitialValues = new ContentValues();
		if (null != pUsernamecn && pUsernamecn.length() >0) {
			lInitialValues.put(KEY_USERNAMECN, pUsernamecn);
		}
		if (null != pPwd && pPwd.length() > 0) {
			lInitialValues.put(KEY_USERPASSWORD, pPwd);
		}
		if (null != pDepId && pDepId.length() >0) {
			lInitialValues.put(KEY_DEPARTMARTID, pDepId);
		}
		if (null != pUserlayer && null != pDepId && pDepId.length() >0) {
			lInitialValues.put(KEY_USERLAYER, pUserlayer);
		}
		if (null != pMemeberinfos && pMemeberinfos.length() > 0) {
			lInitialValues.put(KEY_MEMBERINFOS, pMemeberinfos);
		}
		if (null != pDeviceno && pDeviceno.length() > 0) {
			lInitialValues.put(KEY_DEVICENO, pDeviceno);
		}
		if (writeDB == null)
			writeDB = dbHelper.getWritableDatabase();
		return writeDB.update(TABLE_USERINFO, lInitialValues, PK_USERNAME + "=?", params) > 0;
	}

}